<template>
  <div class="kb-page">
    <!-- 页面头部 -->
    <div class="kb-header">
      <h1 class="page-title">📚 知识库管理</h1>
      <div class="header-actions">
        <el-button @click="refreshStats" :loading="statsLoading" :icon="Refresh">刷新统计</el-button>
      </div>
    </div>

    <!-- 统计信息卡片 -->
    <div class="stats-card" v-if="kbStore.stats">
      <div class="stat-item">
        <div class="stat-value">{{ kbStore.documentCount }}</div>
        <div class="stat-label">文档总数</div>
      </div>
      <div class="stat-item">
        <div class="stat-value">{{ formatSize(kbStore.stats.vectorstore_size_mb) }}</div>
        <div class="stat-label">向量库大小</div>
      </div>
      <div class="stat-item">
        <div class="stat-value">{{ kbStore.typeDistribution.length }}</div>
        <div class="stat-label">文档类型</div>
      </div>
    </div>

    <div class="kb-container">
      <!-- 左侧功能菜单 -->
      <div class="kb-sidebar">
        <div class="menu-title">功能列表</div>
        <div class="menu-list">
          <div
            v-for="item in menuItems"
            :key="item.id"
            :class="['menu-item', { active: activeTab === item.id }]"
            @click="activeTab = item.id"
          >
            <el-icon class="menu-icon">
              <component :is="item.icon" />
            </el-icon>
            <span class="menu-text">{{ item.label }}</span>
            <span v-if="item.badge" class="badge">{{ item.badge }}</span>
          </div>
        </div>
      </div>

      <!-- 右侧内容区域 -->
      <div class="kb-main">
        <!-- 文档上传页面 -->
        <DocumentUpload v-show="activeTab === 'upload'" />

        <!-- 文档列表页面 -->
        <DocumentList v-show="activeTab === 'list'" />

        <!-- 统计页面 -->
        <KBStats v-show="activeTab === 'stats'" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Refresh, Upload, List, PieChart } from '@element-plus/icons-vue'
import { useKnowledgeBaseStore } from '@/stores/knowledge-base'
import DocumentUpload from './knowledge-base/DocumentUpload.vue'
import DocumentList from './knowledge-base/DocumentList.vue'
import KBStats from './knowledge-base/KBStats.vue'

const route = useRoute()
const kbStore = useKnowledgeBaseStore()

const activeTab = ref('list')
const statsLoading = ref(false)

// 监听路由查询参数，自动切换标签页
watch(() => route.query.tab, (newTab) => {
  if (newTab && ['list', 'upload', 'stats'].includes(newTab)) {
    activeTab.value = newTab
  }
}, { immediate: true })

const menuItems = [
  { id: 'list', label: '文档列表', icon: 'List', badge: '' },
  { id: 'upload', label: '上传文档', icon: 'Upload', badge: '' },
  { id: 'stats', label: '统计分析', icon: 'PieChart', badge: '' },
]

const refreshStats = async () => {
  statsLoading.value = true
  try {
    await kbStore.fetchStats()
    ElMessage.success('统计信息已更新')
  } catch (err) {
    ElMessage.error('更新统计信息失败')
  } finally {
    statsLoading.value = false
  }
}

const formatSize = (mb) => {
  if (!mb) return '0 MB'
  return `${mb.toFixed(2)} MB`
}

onMounted(async () => {
  try {
    await kbStore.fetchDocuments()
    await kbStore.fetchStats()
  } catch (err) {
    console.error('初始化知识库页面失败:', err)
  }
})
</script>

<style scoped>
.kb-page {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #f5f7fa;
}

html.dark .kb-page {
  background: #0a0a0a;
}

/* 头部 */
.kb-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 30px;
  background: white;
  border-bottom: 1px solid #e5e7eb;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

html.dark .kb-header {
  background: #1a1a1a;
  border-bottom-color: #333;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

html.dark .page-title {
  color: #e4e4e7;
}

.header-actions {
  display: flex;
  gap: 10px;
}

/* 统计信息卡片 */
.stats-card {
  display: flex;
  gap: 15px;
  padding: 20px 30px;
  background: white;
  border-bottom: 1px solid #e5e7eb;
}

html.dark .stats-card {
  background: #1a1a1a;
  border-bottom-color: #333;
}

.stat-item {
  flex: 1;
  padding: 15px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px;
  text-align: center;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
}

/* 容器布局 */
.kb-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  gap: 0;
}

/* 左侧菜单 */
.kb-sidebar {
  width: 200px;
  background: white;
  border-right: 1px solid #e5e7eb;
  padding: 20px 0;
  overflow-y: auto;
}

html.dark .kb-sidebar {
  background: #1a1a1a;
  border-right-color: #333;
}

.menu-title {
  padding: 15px 20px 10px;
  font-size: 12px;
  font-weight: 600;
  color: #9ca3af;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

html.dark .menu-title {
  color: #64748b;
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 8px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  color: #6b7280;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  border-radius: 6px;
  user-select: none;
}

.menu-item:hover {
  background: #f3f4f6;
  color: #667eea;
  transform: translateX(2px);
}

html.dark .menu-item {
  color: #a1a5b0;
}

html.dark .menu-item:hover {
  background: rgba(129, 140, 248, 0.1);
  color: #818cf8;
}

.menu-item.active {
  background: linear-gradient(90deg, #667eea 0%, #667eea 3px, rgba(102, 126, 234, 0.1) 100%);
  color: #667eea;
  font-weight: 500;
  box-shadow: 2px 0 0 #667eea inset;
}

html.dark .menu-item.active {
  background: linear-gradient(90deg, #818cf8 0%, #818cf8 3px, rgba(129, 140, 248, 0.15) 100%);
  color: #818cf8;
  box-shadow: 2px 0 0 #818cf8 inset;
}

.menu-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.menu-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.badge {
  margin-left: auto;
  background: #ef4444;
  color: white;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}

/* 主内容区域 */
.kb-main {
  flex: 1;
  overflow-y: auto;
  padding: 30px;
}

html.dark .kb-main {
  background: #0a0a0a;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .kb-header {
    padding: 15px 20px;
  }

  .stats-card {
    padding: 15px 20px;
    gap: 10px;
  }

  .kb-sidebar {
    width: 150px;
  }

  .kb-main {
    padding: 20px;
  }

  .page-title {
    font-size: 20px;
  }
}

@media (max-width: 768px) {
  .kb-page {
    flex-direction: column;
  }

  .kb-container {
    flex-direction: column;
  }

  .kb-sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid #e5e7eb;
    padding: 15px 0;
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
  }

  .menu-list {
    flex-direction: row;
  }

  .menu-item {
    white-space: nowrap;
  }

  .stats-card {
    flex-direction: column;
  }

  .kb-main {
    padding: 15px;
  }
}
</style>

